package com.onlinefurnitureshop.management.service.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.shiro.authz.permission.WildcardPermission;
import org.apache.shiro.util.StringUtils;

import com.lowagie.tools.concat_pdf;
import com.onlinefurnitureshop.management.dao.ResourceDao;
import com.onlinefurnitureshop.management.model.Resource;
import com.onlinefurnitureshop.management.service.ResourceService;

public class ResourceServiceImpl implements ResourceService{
	
	private ResourceDao resourceDao;
	
	
	
	public ResourceDao getResourceDao() {
		return resourceDao;
	}

	public void setResourceDao(ResourceDao resourceDao) {
		this.resourceDao = resourceDao;
	}

	@Override
	public Resource createResource(Resource resource) {
		// TODO Auto-generated method stub
		return resourceDao.createResource(resource);
	}

	@Override
	public Resource updateResource(Resource resource) {
		// TODO Auto-generated method stub
		return resourceDao.updateResource(resource);
	}

	@Override
	public void deleteResource(Long resourceId) {
		// TODO Auto-generated method stub
		resourceDao.deleteResource(resourceId);
	}

	@Override
	public Resource findOne(Long resourceId) {
		// TODO Auto-generated method stub
		return resourceDao.findOne(resourceId);
	}

	@Override
	public List<Resource> findAll() {
		// TODO Auto-generated method stub
		return resourceDao.findAll();
	}

	@Override
	public Set<String> findPermission(Set<Long> resourceIds) {
		// TODO Auto-generated method stub
		Set<String> permissions = new HashSet<String>();
		for(Long resourceId:resourceIds){
			Resource resource = findOne(resourceId);
			if(resource!=null&&resource.getPermission()!=null)
				permissions.add(resource.getPermission());
		}
		return permissions;
	}

	@Override
	public List<Resource> findMenus(Set<String> permissions) {
		// TODO Auto-generated method stub
		List<Resource> allResource=findAll();
		List<Resource> menus = new ArrayList<Resource>();
		for(Resource resource:allResource){
			if(!resource.getType().equals("menu"))
				continue;
			if(!hasPermission(permissions, resource))
				continue;
			menus.add(resource);
		}
		return menus;
	}
	
	private boolean hasPermission(Set<String>permissions,Resource resource){
		if(resource.getPermission()==null||resource.getPermission().isEmpty())
			return true;
		for(String permission:permissions){
			WildcardPermission p1 = new WildcardPermission(permission);
			WildcardPermission p2 = new WildcardPermission(resource.getPermission());
			if(p1.implies(p2)||p2.implies(p1))
				return true;
		}
		return false;
	}

	@Override
	public List<Resource> findAllMenus() {
		// TODO Auto-generated method stub
		return resourceDao.findAllMenus();
	}

	@Override
	public List<Resource> findMenusByPid(Long pid) {
		// TODO Auto-generated method stub
		return resourceDao.findMenusByPid(pid);
	}

	@Override
	public List<Resource> findRootMenu() {
		// TODO Auto-generated method stub
		return resourceDao.findRootMenu();
	}

}
